package cn.tedu.csmall.sso.controller;

import cn.tedu.csmall.commons.pojo.admin.dto.AdminAddDTO;
import cn.tedu.csmall.commons.pojo.admin.dto.AdminLoginDTO;
import cn.tedu.csmall.commons.pojo.admin.vo.AdminListVO;
import cn.tedu.csmall.commons.restful.JsonResult;
import cn.tedu.csmall.commons.restful.ResponseCode;
import cn.tedu.csmall.sso.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admins")
@Api(tags = "管理员模块")
@Slf4j
public class AdminController {

    @Autowired
    IAdminService adminService;

    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
//    /ams/admin/update
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult addNew(@RequestBody AdminAddDTO adminAddDTO){
//        if (!adminAddDTO.getPassword().equals(adminAddDTO.getPasswordAct())){
//            return JsonResult.failed(ResponseCode.BAD_REQUEST,"密码不一致");
//        }
        //处理请求
        //设置角色 ==  2
        adminService.addNew(adminAddDTO);
        return JsonResult.ok("添加管理员成功");
    }

    @GetMapping("/list")
    @ApiOperation("查询管理员列表")
//    正确的权限请求
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    //    不正确的权限请求
   // @PreAuthorize("hasAuthority('/ams/admin/aaa')")
    public JsonResult<List<AdminListVO>> getList(){
        List<AdminListVO> list = adminService.list();
        return JsonResult.ok("查询成功",list);
    }


    @PostMapping("/login")
    @ApiOperation("管理员登陆")
    public JsonResult login(@RequestBody AdminLoginDTO adminLoginDTO){
        log.info("接受到的登录参数：{}",adminLoginDTO);
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok("登陆成功",jwt);
    }



}
